#!/usr/bin/env python

import os
import sys
import string

segment_separator = '\r'

file_in = os.fdopen(0, 'rb')

everything = file_in.read()

# print 'lf lines =', len(everything.split('\012'))
# print 'cr lines =', len(everything.split('\015'))
lines = string.split(everything, segment_separator)


field_separator = '|'

have_message = 0

for line in lines:
    # print 'line =', line
    line = string.strip(line)
    if 0 == len(line):
        continue

    if line[0] in ('"', '\x0b'):
        line = line[1:]
    if '"' == line[-1]:
        line = line[:-1]

    if line[:3] == 'MSH':
        if have_message:
            print
            print

        have_message = 1
        field_separator = line[3]

        #print 'raw MSH: ' + line[3:-1]

        name = 'MSH'
        fields = string.split(line[3:], field_separator)
        fields[1:1] = [field_separator]

    elif have_message:
        stuff = string.split(line, '" "')
        if 2 == len(stuff):
            (name, rest) = stuff
        else:
            # add a field_separator to get the field count right
            (name, rest) = (line[:3], field_separator + line[4:])

        fields = string.split(rest, field_separator)


    if have_message:
        print
        print name + ':'
        for i in range(1, len(fields)):
            if 0 < len(fields[i]):
                print '   %3d: %s' % (i, fields[i])
            
                
